#!/bin/sh
# chkconfig: 2345 56 26
# description: biz Service

LOG_FILE="/home/log/biz_service.log"

# 检查端口是否被占用
check_port() {
    local port=$1
    local process_name=$2

    # 检查端口是否被占用
    if lsof -i :$port > /dev/null; then
        echo "Port $port is already in use by $process_name." >> $LOG_FILE
        return 0
    else
        echo "Port $port is not in use by $process_name." >> $LOG_FILE
        return 1
    fi
}

# 等待端口可用
wait_for_port() {
    local port=$1
    local process_name=$2
    local max_retries=50
    local retries=0

    until [ $retries -ge $max_retries ]; do
        if check_port $port $process_name; then
            echo "$(date): Port $port is now available for $process_name." >> $LOG_FILE
            return 0
        fi
        echo "$(date): 等待端口 $port for $process_name..." >> $LOG_FILE
        retries=$((retries + 1))
        sleep 1
    done

    echo "$(date): Error: Failed to wait for port $port to become available after $max_retries attempts." >> $LOG_FILE
    return 1
}

# 检查服务状态
check_service_status() {
    local service_name=$1
    if systemctl is-active --quiet $service_name; then
        echo "$(date): $service_name is active and running." >> $LOG_FILE
        return 0
    else
        echo "$(date): $service_name is not active." >> $LOG_FILE
        return 1
    fi
}

# 启动服务
start_services() {
    # 检查 MySQL 服务状态
    if ! check_service_status mysqld; then
        echo "$(date): 正在 MySQL..." >> $LOG_FILE
        if ! service mysql start; then
            echo "$(date): Error MySQL.启动出错" >> $LOG_FILE
            exit 1
        fi
        if ! wait_for_port 3306 mysql; then
            echo "$(date): MySQL did not start within the expected time." >> $LOG_FILE
            exit 1
        fi
    else
        echo "$(date): MySQL is already running." >> $LOG_FILE
    fi

    # 检查 Redis 服务状态
    if ! check_service_status redis; then
        echo "$(date): Starting Redis..." >> $LOG_FILE
        if ! service redis start; then
            echo "$(date): Error starting Redis." >> $LOG_FILE
            exit 1
        fi
        if ! wait_for_port 6379 redis; then
            echo "$(date): Redis did not start within the expected time." >> $LOG_FILE
            exit 1
        fi
    else
        echo "$(date): Redis is already running." >> $LOG_FILE
    fi

    # 启动 powerjob
    if ! java-service powerjob_server_jar start; then
        echo "$(date): Error starting powerjob_server_jar." >> $LOG_FILE
        exit 1
    fi

    # 等待 powerjob 启动完成
    if ! wait_for_port 7700 powerjob_server_jar; then
        echo "$(date): Powerjob did not start within the expected time." >> $LOG_FILE
        exit 1
    fi
    sleep 10s # 确保 powerjob_server_jar 完全启动

    # 启动其他服务
    if ! java-service monitor_admin_jar start || \
       ! java-service rtsp2flv_0_0_1_jar start || \
       ! java-service rtsp2flv_0_0_2_jar start || \
       ! java-service rtsp2flv_0_0_3_jar start; then
        echo "$(date): Error starting other services." >> $LOG_FILE
        exit 1
    fi

    echo "$(date): All services started successfully." >> $LOG_FILE
}

# 重启服务
restart_services() {
    # 等待 powerjob 启动完成
    if ! wait_for_port 7700 powerjob_server_jar; then
        echo "$(date): Powerjob did not start within the expected time." >> $LOG_FILE
        exit 1
    fi
    sleep 10s # 确保 powerjob_server_jar 完全启动

    if ! java-service trainGround10086_jar start || \
       ! java-service trainGround10087_jar start || \
       ! java-service trainGround10088_jar start || \
       ! java-service trainGround10089_jar start || \
       ! java-service trainGround10090_jar start; then
        echo "$(date): Error restarting services." >> $LOG_FILE
        exit 1
    fi

    echo "$(date): All services restarted successfully." >> $LOG_FILE
}

# 主程序
if start_services; then
    echo "$(date): Services started successfully." >> $LOG_FILE
else
    echo "$(date): Error starting services." >> $LOG_FILE
    exit 1
fi

if restart_services; then
    echo "$(date): Services restarted successfully." >> $LOG_FILE
else
    echo "$(date): Error restarting services." >> $LOG_FILE
    exit 1
fi
